﻿/*
 * HostInfoData.java
 *
 * Created on 2007年10月22日, 下午 4:36
 *
 */

package org.uranus.core.dataHandler;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
/**
 * Copyright 2007 Hsiang-Yu Wang<br>
 * This file is distributed under the terms of the GNU General Public License<br>
 *<br>
 *<pre>
 * --------------------License notice--------------------
 * GNU GENERAL PUBLIC LICENSE
 * Version 3, 29 June 2007
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not,
 * visit: http://www.gnu.org/licenses/gpl.txt
 * --------------------License notice--------------------</pre>
 *
 * @author Hsiang-Yu Wang
 */
public class HostInfoData extends RMS{
    private String hostName;
    private String address;
    private String protocol;
    private int port;
    private String id;
    private String pwd;
    
    /** Creates a new instance of HostInfoData */
    public HostInfoData(String hostName, boolean isNew) {
        super.rmsName = "_host_";
        rmsName += hostName;
        this.setHostName(hostName);
        if(!isNew){
           load(rmsName); 
        }
    }
    public HostInfoData(String hostName, String address, String protocol, int port, String id,String pwd){
        this.setAddress(address);
        this.setHostName(hostName);
        this.setProtocol(protocol);
        super.rmsName = "_host_";
        rmsName += hostName;
        this.setPort(port);
        if(id.length() > 0){
            this.setId(id);
            this.setPwd(pwd);
        }
        this.save();
    }
    public void write(DataOutputStream out) throws IOException {
        out.writeUTF(getAddress());
        out.writeUTF(getProtocol());
        out.writeInt(getPort());
        if( getId() != null ){
            out.writeUTF(getId());
            if( getPwd() != null){
                out.writeUTF(getPwd());
            }
        }
    }
    
    public void read(DataInputStream in) throws IOException {
        setAddress(in.readUTF());
        setProtocol(in.readUTF());
        setPort(in.readInt());
        setId((in.available() == 0)? null : in.readUTF());
        setPwd((getId() == null) ? null : ((in.available() == 0)? null : in.readUTF()));
    }
    
    public void save() {
        save(rmsName);
    }
    
    public String getAddress() {
        return address;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    public String getProtocol() {
        return protocol;
    }
    
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }
    
    public int getPort() {
        return port;
    }
    
    public void setPort(int port) {
        this.port = port;
    }
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getPwd() {
        return pwd;
    }
    
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }
    
}
